/**
 * @Title: CustomerPayCardServiceImpl.java
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月20日
 */
package com.fortune.rainshinesystem.service.impl;

import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ErrorCodes;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.enums.SmsTypeEnum;
import com.fortune.rainshinecommon.model.CustomerModel;
import com.fortune.rainshinecommon.model.SmsParamsModel;
import com.fortune.rainshinecommon.model.system.CardModel;
import com.fortune.rainshinecommon.model.system.CustomerPayCardModel;
import com.fortune.rainshinecommon.model.system.CustomerServiceTotalModel;
import com.fortune.rainshinesystem.mapper.CardMapper;
import com.fortune.rainshinesystem.mapper.CustomerMapper;
import com.fortune.rainshinesystem.mapper.CustomerPayCardMapper;
import com.fortune.rainshinesystem.mapper.CustomerServiceTotalMapper;
import com.fortune.rainshinesystem.service.CustomerPayCardService;
import com.fortune.rainshinesystem.service.SmsFeign;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CustomerPayCardServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年5月20日
 *
 */
@Service
public class CustomerPayCardServiceImpl implements CustomerPayCardService {
    private static final Logger logger = LoggerFactory.getLogger(CustomerPayCardServiceImpl.class);

    @Autowired
    private CustomerPayCardMapper mapper;
    @Autowired
    private CustomerServiceTotalMapper serviceTotalMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SmsFeign smsFeign;
    @Autowired
    private CardMapper cardMapper;

    @Override
    public MsgModel add(CustomerPayCardModel model) {

        try {
            //判断是否存在总记录数
            CustomerServiceTotalModel total = new CustomerServiceTotalModel();
            total.setCustomerId(model.getCustomerId());
            total.setShopId(model.getShopId());
            total.setCardId(model.getCardId());
            CustomerServiceTotalModel total1 = serviceTotalMapper.selectOne(total);
            if (total1 != null) {
                if (total1.getServiceTimes() > 0) {
                    //服务次数减1
                    total1.setServiceTimes(total1.getServiceTimes() - 1);
                    //更新
                    int u = serviceTotalMapper.updateAllColumnById(total1);
                    if(u == 1){
                        //插入服务记录
                        model.setTotalId(total1.getId());
                        int s = mapper.insert(model);
                        if(s == 1){
                            //发送会员服务通知短信
                            CardModel card = cardMapper.selectById(model.getCardId());
                            CustomerModel cm = customerMapper.selectById(model.getCustomerId());
                            SmsParamsModel smsParams = new SmsParamsModel();
                            smsParams.setSmsType(SmsTypeEnum.serviceNote.getKey());
                            smsParams.setName(card.getCardName());
                            CustomerServiceTotalModel cstm = serviceTotalMapper.selectById(total1.getId());
                            smsParams.setCount(cstm.getServiceTimes()!=null?cstm.getServiceTimes().toString():"0");
                            Gson gson = new Gson();
                            String jsonParams = gson.toJson(smsParams);
                            smsFeign.sendSms(cm.getCustomerPhone(), jsonParams);
                            return new MsgModel("会员服务成功！", ResultCode.SUCCESS);
                        }else{
                            return new MsgModel("会员服务异常！", ResultCode.FAILURE);
                        }
                    }else{
                        return new MsgModel("会员服务异常！", ResultCode.FAILURE);
                    }
                } else {
                    return new MsgModel("服务次数已用完，请及时充值！", ResultCode.FAILURE);
                }
            } else {
                return new MsgModel("未查询到该客户的此类会员卡", ResultCode.FAILURE);
            }
        } catch (Exception e) {
            logger.error(ErrorCodes.BUSINESS_EXCEPTION.getMessage(), e);
            return new MsgModel("会员服务失败！", ResultCode.FAILURE);
        }
    }

    @Override
    public MsgModel list(ParmsModel parm) {
        try {
        	parm.setPage((parm.getPage()-1)*parm.getLimit());
        	parm.setLimit(parm.getPage()*parm.getLimit()+parm.getLimit());
            List<CustomerServiceTotalModel> list = serviceTotalMapper.getAllRecords(parm);
            int total = serviceTotalMapper.getAllRecordsCount(parm);
            return new MsgModel(total, list);
        } catch (Exception e) {
            logger.error(ErrorCodes.BUSINESS_EXCEPTION.getMessage(), e);
            return new MsgModel(ResultCode.FAILURE);
        }
    }

    @Override
    public List<CustomerPayCardModel> listRecord(String id) {
        List<CustomerPayCardModel> list = new ArrayList<>();
        try {
            list = mapper.getAllRecordsByTotalId(id);
        } catch (Exception e) {
            logger.error(ErrorCodes.BUSINESS_EXCEPTION.getMessage(), e);
        }
        return list;
    }

}
